var app = getApp(),
  $ = require("../../utils/httpHelper.js")
Page({
  data: {
    hasAddress: false,
    addr: '',
    good_info: '',
    good_num: '',
    sell_price: 0,
    total_fee: 0,
    real_fee: 0,
    good_id: '',
    liuyan: '',
    pay_cash: false,
    carlist: [],
    fromcar: false,
    shopname: '',
    coupon_list: [],
    fee: 0,
    coupon_id: '',
    sku_id: ''
  },

  onLoad: function (options) {
    var that = this;
    console.log(options)
    var rd_session = wx.getStorageSync('rd_session');
    this.setData({
      shopname: wx.getStorageSync('shop_name')
    })
    if (!app.check_empty(options.from) && options.from == 'car') {
      $.httpPost("cart_list", { rd_session: rd_session, coupon_id: this.data.coupon_id, status: 1 }, (data, err) => {
        this.setData({
          carlist: data.data.list,
          total_fee: data.data.total_fee,
          real_fee: data.data.real_fee,
          good_num: data.data.ctotal_num,
          fromcar: true,
        })
      })
    } else {
      if (!app.check_empty(options.sku_id)) {
        let par = {
          good_id: options.good_id,
          num: options.num,
          sku_id: options.sku_id,
          rd_session: rd_session
        }
        $.httpPost("pay_info", par, (data, err) => {
          this.setData({
            good_info: data.data,
            good_num: options.num,
            good_id: options.good_id,
            sell_price: data.data.sell_price,
            total_fee: data.data.total_fee,
            real_fee: data.data.real_fee,
            sku_id: options.sku_id
          })
        })
      } else {
        let par = {
          good_id: options.good_id,
          num: options.num,
          rd_session: rd_session
        }
        $.httpPost("pay_info", par, (data, err) => {
          this.setData({
            good_info: data.data,
            good_num: options.num,
            good_id: options.good_id,
            sell_price: data.data.sell_price,
            total_fee: data.data.total_fee,
            real_fee: data.data.real_fee
          })
        })
      }
    }
  },
  message(e) {
    this.setData({
      liuyan: e.detail.value
    })
  },
  choseyhq() {
    let par = {
      rd_session: app.globalData.userInfo.rd_session,
      page: 1,
      pagesize: 100
    }
    $.httpPost("member_coupon", par, (data, err) => {
      this.setData({
        coupon_list: data.list,
        yhqpop: true
      })
    })
    // this.setData({
    //   yhqpop: true
    // })
  },
  radioChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value);
    var arr = e.detail.value.split('-')
    if (arr[0] <= this.data.total_fee) {
      if (this.data.fromcar) {
        $.httpPost("cart_list", { rd_session: app.globalData.userInfo.rd_session, coupon_id: arr[2], status: 1 }, (data, err) => {
          if (data.result == 'success') {
            this.setData({
              carlist: data.data.list,
              total_fee: data.data.total_fee,
              real_fee: data.data.real_fee,
              good_num: data.data.ctotal_num,
              fromcar: true,
              fee: arr[1],
              coupon_id: arr[2]
            })
          } else {
            wx.showToast({
              title: data.message,
            })
            this.setData({
              fee: 0,
            })
          }

        })
      } else {
        let par = {
          good_id: this.data.good_id,
          num: this.data.good_num,
          sku_id: this.data.sku_id,
          coupon_id: arr[2],
          rd_session: app.globalData.userInfo.rd_session
        }
        $.httpPost("pay_info", par, (data, err) => {
          if (data.result == 'success') {
            this.setData({
              good_info: data.data,
              sell_price: data.data.sell_price,
              total_fee: data.data.total_fee,
              real_fee: data.data.real_fee,
              fee: arr[1],
              coupon_id: arr[2]
            })
          } else {
            wx.showToast({
              title: data.message,
            })
            this.setData({
              fee: 0,
            })
          }

        })
      }

    } else {
      wx: wx.showToast({
        title: '不满足满减金额',
        duration: 2000,
      })
    }
    this.yhqHide()
  },
  yhqHide() {
    this.setData({
      yhqpop: false
    })
  },
  topay() {
    if (this.data.pay_cash) {
      this.submit()
    } else {
      //余额
      if (app.check_empty(this.data.password)) {
        wx.showToast({
          title: '请输入密码',
          duration: 2000
        })
      } else {
        this.submit()
      }
    }
  },

  subcheck(){
    console.log('che')
    $.httpPost("member_info", { rd_session: app.globalData.userInfo.rd_session }, (data, err) => {
      if (data.data.data.name == null) {
        wx.showModal({
          title: '提示',
          content: '请先在【我的】->【会员信息】完善信息~',
        })
      } else {
        this.submit()
      }
    })
  },

  submit() {
    
    if (this.data.fromcar) {
      //来自购物车的订单
      let params = {
        message: this.data.liuyan,
        rd_session: app.globalData.userInfo.rd_session,
        sku_id: this.data.sku_id,
        shop_id: wx.getStorageSync("shop_id")
      }
      $.httpPost("submit_cart_order", params, (data, err) => {
        //去支付
        if (data.result == 'success') {
          app.wxPayGood({
            order_no: data.order_no, openid: app.globalData.userInfo.openId,
            success: function () {
              wx.showToast({
                title: '支付成功哈',
              })
              wx.switchTab({
                url: '/pages/mine/mine',
              })
            },
            fail: function () {
              wx.showToast({
                title: '支付失败了呢',
              })
            }
          })
        }else{
          wx.showToast({
            title: data.message,
          })
        }
      })
    } else {
      let params = {
        message: this.data.liuyan,
        rd_session: app.globalData.userInfo.rd_session,
        total_num: this.data.good_num,
        good_id: this.data.good_id,
        sku_id: this.data.sku_id,
        coupon_id: this.data.coupon_id,
        shop_id: wx.getStorageSync("shop_id")
      }
      $.httpPost("submit_order", params, (data, err) => {
        if (data.result == 'success') {
          app.wxPayGood({
            order_no: data.order_no, openid: app.globalData.userInfo.openId,
            success: function () {
              wx.showToast({
                title: '支付成功哈',
              })
              wx.switchTab({
                url: '/pages/mine/mine',
              })
            },
            fail: function () {
              wx.showToast({
                title: '支付失败了呢',
              })
            }
          })
        }
      })
    }

  }

})  